package com.chinaoly.sdk.util;



import com.chinaoly.sdk.data.AbnormalConsumer;
import com.chinaoly.sdk.data.DataCallBackListener;
import com.chinaoly.sdk.data.NormalConsumer;
import com.chinaoly.sdk.data.entity.BaseResult;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by chenjian on 2019/1/25 11:13.
 */
public class ObservableUtil {
    /**
     * 结束事件
     */
    public static void disposeEvent(Disposable disposable) {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
    }

    /**
     * observable 转换
     * @param observable
     * @param <T>
     * @return
     */
    public static <T> Observable<T> transform(Observable<T> observable) {
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     *  observable 转换 成dispose
     * @param observable
     * @param l
     * @param <T>
     * @return
     */
    public static <T> Disposable transToDispose(Observable<BaseResult<T>> observable, DataCallBackListener<BaseResult<T>> l) {
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NormalConsumer<>(l), new AbnormalConsumer(l));

    }
}
